/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package justdoit.view;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import justdoit.view.model.TasksTableModel;
import javax.swing.JScrollPane;
import justdoit.controller.TaskManager;
import justdoit.model.Task;
import justdoit.model.User;
import justdoit.view.model.TaskListModel;

/**
 * The Panel to place the TaskTable, which is to display some tasks
 * @author Şadan Ekdemir & Can
 */
public class TaskTablePanel extends JScrollPane {

    public static final String MODE_TABLE = "table";
    public static final String MODE_LIST = "list";
    public static final String VIEW_PENDING = "pending";
    public static final String VIEW_COMPLETED = "completed";
    public static final String VIEW_OVERDUE = "overdue";
    private String view;
    private TaskTable taskTable;
    private TaskList taskList;
    private String mode;
    private User user;

    public TaskTablePanel(User user, String mode, String view) {
        this.user = user;
        this.mode = mode;
        this.view = view;
        taskTable = new TaskTable(
                new TasksTableModel(TaskManager.getPendingTasks()), this.user);
        taskList = new TaskList(new TaskListModel(TaskManager.getPendingTasks()));
        this.add(taskTable);
        this.add(taskList);
        this.setMode(mode);
        this.changeView(view);
    }

    public TaskTable getTaskTable() {
        return taskTable;
    }
    // set the background for everything inside the panel

    public void setTaskTableListBackground(Color bg) {
        taskTable.setBackground(bg);
        taskList.setBackground(bg);
        this.setBackground(bg);
    }

    // set the mode: table / list
    public final void setMode(String mode) {
        if (mode.equals(MODE_LIST)) {
            this.setViewportView(taskList);
        } else {
            this.setViewportView(taskTable);
        }
        this.mode = mode;
    }

    // change the view: pending/completed/overdue/... defined somewhere. :)
    public final void changeView(String view) {
        refreshData(getTasksByView(view));
        this.view = view;
    }

    // get the specific tasks from TaskManager, according to different views
    private List<Task> getTasksByView(String view) {
        List<Task> tasks;
        if (view.equals(VIEW_COMPLETED)) {
            tasks = TaskManager.getCompletedTasks();
        } else if (view.equals(VIEW_OVERDUE)) {
            tasks = TaskManager.getOverdueTasks();
        } else if (view.equals(VIEW_PENDING)) {
            tasks = TaskManager.getPendingTasks();
        } else {
            System.out.println("Error!");
            tasks = new ArrayList<Task>();
        }
        return tasks;
    }

    private void refreshData(List<Task> tasks) {
        if (this.mode.equals(MODE_TABLE)) {
            taskTable.refresh(tasks);
        } else {
            ((TaskListModel) taskList.getModel()).setTasks(tasks);
            taskList.updateUI();
        }
    }

    public void refreshData() {
        List<Task> tasks = getTasksByView(this.view);
        refreshData(tasks);
    }

    // return the selected Task object 
    // return the first selection if multiply selection,
    public Task getSelectedTask() {
        if (this.mode.equals(MODE_TABLE)) {
            return ((TasksTableModel) taskTable.getModel()).getTask(taskTable.getSelectedRow());
        } else {
            return ((TaskListModel) taskList.getModel()).getTask(taskList.getSelectedIndex());
        }
    }

    // return null if there's no next
    public Task getNextTask() {
        if (this.mode.equals(MODE_TABLE)) {
            if (taskTable.getSelectedRow() >= (taskTable.getRowCount() - 1)) {
                return null;
            }
            taskTable.changeSelection(taskTable.getSelectedRow() + 1, 1, false, false);
            return ((TasksTableModel) taskTable.getModel()).getTask(taskTable.getSelectedRow());
        } else {
            if (taskList.getSelectedIndex() >= (taskList.getVisibleRowCount() - 1)) {
                return null;
            }
            taskList.setSelectedIndex(taskList.getSelectedIndex() + 1);
            return ((TaskListModel) taskList.getModel()).getTask(taskList.getSelectedIndex());
        }
    }

    // return null if there's no further previous
    public Task getPreviousTask() {
        if (this.mode.equals(MODE_TABLE)) {
            if (taskTable.getSelectedRow() <= 0) {
                return null;
            }
            taskTable.changeSelection(taskTable.getSelectedRow() - 1, 1, false, false);
            return ((TasksTableModel) taskTable.getModel()).getTask(taskTable.getSelectedRow());
        } else {
            if (taskList.getSelectedIndex() <= 0) {
                return null;
            }
            taskList.setSelectedIndex(taskList.getSelectedIndex() - 1);
            return ((TaskListModel) taskList.getModel()).getTask(taskList.getSelectedIndex());
        }
    }
}
